package com.demo.flowable;

import com.demo.model.CovidForm;
import com.demo.model.HisForm;
import com.demo.model.TreatmentForm;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.FormService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.form.api.FormInfo;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class FlowableHisService {

    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource(name = "formServiceBean")
    private FormService formService;

    /**
     * 体温<=37度
     * @param hisForm
     */
    public void cold(HisForm hisForm) {
        //流程定义的key,对应请假的流程图
        String processDefinitionKey = "his";
        //业务代码，根据自己的业务用
        String businessKey = "his";

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
        log.info("启动成功:{}", processInstance.getId());

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        // 申领任务
        taskService.claim(task.getId(), "my");
        taskService.complete(task.getId(), hisForm.toMap());
    }

    public void hot(HisForm hisForm, CovidForm covidForm) {
        //流程定义的key,对应请假的流程图
        String processDefinitionKey = "his";
        //业务代码，根据自己的业务用
        String businessKey = "his";

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
        log.info("启动成功:{}", processInstance.getId());

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        // 申领任务
        taskService.claim(task.getId(), "my");
        taskService.complete(task.getId(), hisForm.toMap());

        task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        taskService.claim(task.getId(), "my");
        taskService.complete(task.getId(), covidForm.toMap());
    }

    public void treatment(HisForm hisForm, CovidForm covidForm, TreatmentForm treatmentForm) {
        //流程定义的key,对应请假的流程图
        String processDefinitionKey = "his";
        //业务代码，根据自己的业务用
        String businessKey = "his";

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
        log.info("启动成功:{}", processInstance.getId());

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        // 申领任务
        taskService.claim(task.getId(), "my");
        taskService.complete(task.getId(), hisForm.toMap());

        task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        taskService.claim(task.getId(), "my");
        taskService.complete(task.getId(), covidForm.toMap());

        task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        taskService.claim(task.getId(), "my");
        taskService.complete(task.getId(), treatmentForm.toMap());
    }
}
